package org.springcloud.wisdom.spring_02_classroom.student.choose.controller;

import org.springcloud.wisdom.spring_02_classroom.bean.BaseController;
import org.springcloud.wisdom.spring_02_classroom.bean.ResponseData;
import org.springcloud.wisdom.spring_02_classroom.student.choose.entity.ChooseCourse;
import org.springcloud.wisdom.spring_02_classroom.student.choose.service.ChooseCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/choose")
public class ChooseCourseController extends BaseController {
    @Autowired
    ChooseCourseService chooseCourseService;

    //查看所有课程信息
    @RequestMapping("/all")
    public ResponseData<Object> getPage(@RequestParam("pageNum") int pageNum,
                                        @RequestParam("pageSize") int pageSize){
        return returnObject(chooseCourseService.AllCourse(pageNum,pageSize));
    }

    //选课
    @PostMapping("/course")
    public ResponseData<Boolean> insert(@RequestBody ChooseCourse chooseCourse) {
        boolean result = chooseCourseService.insert(chooseCourse);
        if (!result) {
            String errorMsg = "选课失败，您已经选了同一时间的课程或已经选过这门课程！";
            return new ResponseData<>(400, errorMsg, null);
        }
        // 如果选课成功，返回成功的消息
        return new ResponseData<>(200, "选课成功！", result);
    }
}
